package com.ifp.opengate.boot.bean.access;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.ifp.core.log.Trace;
import com.ifp.opengate.boot.constants.Event;
import com.ifp.opengate.boot.emum.ErrorEnum;

/**
 * @Classname IPBlackAndWhiteBean
 * @Description TODO
 * @Date 2019/4/9 10:56
 * @Created by zzh
 *          <p>
 *          zhuzh@belink.com
 *          </p>
 * @Version 1.0
 */
@ConfigurationProperties("ifp.ip")
@Component
public final class IPBlackAndWhiteBean {

    Map<Object, Object> appWhtelist = new HashMap<>();

    Map<Object, Object> macblackMap = new HashMap<Object, Object>();

    Map<Object, Object> ipaddrblackMap = new HashMap<Object, Object>();

    Map<Object, Object> mobileblackMap = new HashMap<Object, Object>();

    Map<Object, Object> emailblackMap = new HashMap<Object, Object>();

    Map<Object, Object> userIdblackMap = new HashMap<Object, Object>();

    Map<Object, Object> thirdPartyIdblackMap = new HashMap<Object, Object>();

    Map<Object, Object> appIdblackMap = new HashMap<Object, Object>();

    List<String> blackList = new CopyOnWriteArrayList<>();

    List<String> whiteList = new CopyOnWriteArrayList<>();

    public boolean inBlacklist(String type, String contkey) {
        try {
            if (Event.MAC_BLACK_MAP.equalsIgnoreCase(type)) {
                if (macblackMap.containsKey(contkey)) {
                    // 状态为0是启用
                    if (macblackMap.get(contkey).equals("0")) {
                        return true;
                    }
                }
            } else if (Event.MOBILE_BLACK_MAP.equalsIgnoreCase(type)) {
                // mobile
                if (mobileblackMap.containsKey(contkey)) {
                    // 状态为0是启用
                    if (mobileblackMap.get(contkey).equals("0")) {
                        return true;
                    }
                }
            } else if (Event.EMAIL_BLACK_MAP.equalsIgnoreCase(type)) {
                // email
                if (emailblackMap.containsKey(contkey)) {
                    // 状态为0是启用
                    if (emailblackMap.get(contkey).equals("0")) {
                        return true;
                    }
                }
            } else if (Event.USERID_BLACK_MAP.equalsIgnoreCase(type)) {
                // userId
                if (userIdblackMap.containsKey(contkey)) {
                    // 状态为0是启用
                    if (userIdblackMap.get(contkey).equals("0")) {
                        return true;
                    }
                }
            } else if (Event.THIRDPARTYID_BLACK_MAP.equalsIgnoreCase(type)) {
                // thirdPartyId
                if (thirdPartyIdblackMap.containsKey(contkey)) {
                    // 状态为0是启用
                    if (thirdPartyIdblackMap.get(contkey).equals("0")) {
                        return true;
                    }
                }
            } else if (Event.APPID_BLACK_MAP.equalsIgnoreCase(type)) {
                // appId
                if (appIdblackMap.containsKey(contkey)) {
                    // 状态为0是启用
                    if (appIdblackMap.get(contkey).equals("0")) {
                        return true;
                    }
                }
            } else {
                Trace.logInfo(Trace.MODULE_ACTION, "无效的黑名单类型值");
            }
            return false;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Trace.logError(Trace.MODULE_CORE, "查找黑名单出现异常", contkey);
            return false;
        }
    }

    public void clearMap(String key) {
        if (Event.MAC_BLACK_MAP.equalsIgnoreCase(key)) {
            macblackMap.clear();
        } else if (Event.IP_BLACK_MAP.equalsIgnoreCase(key)) {
            ipaddrblackMap.clear();
        } else if (Event.MOBILE_BLACK_MAP.equalsIgnoreCase(key)) {
            mobileblackMap.clear();
        } else if (Event.EMAIL_BLACK_MAP.equalsIgnoreCase(key)) {
            emailblackMap.clear();
        } else if (Event.USERID_BLACK_MAP.equalsIgnoreCase(key)) {
            userIdblackMap.clear();
        } else if (Event.THIRDPARTYID_BLACK_MAP.equalsIgnoreCase(key)) {
            thirdPartyIdblackMap.clear();
        } else if (Event.APPID_BLACK_MAP.equalsIgnoreCase(key)) {
            appIdblackMap.clear();
        } else {
            Trace.logInfo(Trace.MODULE_ACTION, "无效的黑名单类型值");
        }
        Trace.logError(Trace.MODULE_CORE, "黑名单缓存{}清除成功", key);
    }

    public String initOneBlackList(String key, Map<Object, Object> tempMap) {

        try {
            if (Event.MAC_BLACK_MAP.equalsIgnoreCase(key)) {
                macblackMap.clear();// 重新整理
                for (Object item : tempMap.keySet()) {
                    Object value = tempMap.get(item).toString();
                    macblackMap.put(item, value);
                }
            } else if (Event.IP_BLACK_MAP.equalsIgnoreCase(key)) {
                // ip
                ipaddrblackMap.clear();// 重新整理
                for (Object item : tempMap.keySet()) {
                    Object value = tempMap.get(item).toString();
                    ipaddrblackMap.put(item, value);
                }
            } else if (Event.MOBILE_BLACK_MAP.equalsIgnoreCase(key)) {
                // mobile
                mobileblackMap.clear();// 重新整理
                for (Object item : tempMap.keySet()) {
                    Object value = tempMap.get(item).toString();
                    mobileblackMap.put(item, value);
                }
            } else if (Event.EMAIL_BLACK_MAP.equalsIgnoreCase(key)) {
                // email
                emailblackMap.clear();// 重新整理
                for (Object item : tempMap.keySet()) {
                    Object value = tempMap.get(item).toString();
                    emailblackMap.put(item, value);
                }
            } else if (Event.USERID_BLACK_MAP.equalsIgnoreCase(key)) {
                // userId
                userIdblackMap.clear();// 重新整理
                for (Object item : tempMap.keySet()) {
                    Object value = tempMap.get(item).toString();
                    userIdblackMap.put(item, value);
                }
            } else if (Event.THIRDPARTYID_BLACK_MAP.equalsIgnoreCase(key)) {
                // thirdPartyId
                thirdPartyIdblackMap.clear();// 重新整理
                for (Object item : tempMap.keySet()) {
                    Object value = tempMap.get(item).toString();
                    thirdPartyIdblackMap.put(item, value);
                }
            } else if (Event.APPID_BLACK_MAP.equalsIgnoreCase(key)) {
                // appId
                appIdblackMap.clear();// 重新整理
                for (Object item : tempMap.keySet()) {
                    Object value = tempMap.get(item).toString();
                    appIdblackMap.put(item, value);
                }
            } else {
                Trace.logInfo(Trace.MODULE_ACTION, "无效的黑名单类型值");
            }
            Trace.logError(Trace.MODULE_CORE, "黑名单缓存{}添加成功", tempMap);
            return ErrorEnum.success.code();
        } catch (Exception e) {
            Trace.logError(Trace.MODULE_CORE, "黑名单缓存{}添加异常", tempMap);
            return ErrorEnum.error.code();
        }

    }

    public String removeBlackList(String type, String rmkey) {

        try {
            if (Event.MAC_BLACK_MAP.equalsIgnoreCase(type)) {
                macblackMap.remove(rmkey);
            } else if (Event.IP_BLACK_MAP.equalsIgnoreCase(type)) {
                // ip
                ipaddrblackMap.remove(rmkey);
            } else if (Event.MOBILE_BLACK_MAP.equalsIgnoreCase(type)) {
                // mobile
                mobileblackMap.remove(rmkey);
            } else if (Event.EMAIL_BLACK_MAP.equalsIgnoreCase(type)) {
                // email
                emailblackMap.remove(rmkey);
            } else if (Event.USERID_BLACK_MAP.equalsIgnoreCase(type)) {
                // userId
                userIdblackMap.remove(rmkey);
            } else if (Event.THIRDPARTYID_BLACK_MAP.equalsIgnoreCase(type)) {
                // thirdPartyId
                thirdPartyIdblackMap.remove(rmkey);
            } else if (Event.APPID_BLACK_MAP.equalsIgnoreCase(type)) {
                // appId
                appIdblackMap.remove(rmkey);
            } else {
                Trace.logInfo(Trace.MODULE_ACTION, "无效的黑名单类型值");
            }
            Trace.logError(Trace.MODULE_CORE, "黑名单缓存{}清除成功", rmkey);
            return ErrorEnum.success.code();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Trace.logError(Trace.MODULE_CORE, "黑名单缓存{}清除异常", rmkey);
            return ErrorEnum.error.code();
        }

    }

    public Map<Object, Object> getAppWhtelist() {
        return appWhtelist;
    }

    public void setAppWhtelist(Map<Object, Object> appWhtelist) {
        this.appWhtelist = appWhtelist;
    }

    public Map<Object, Object> getMacblackMap() {
        return macblackMap;
    }

    public void setMacblackMap(Map<Object, Object> macblackMap) {
        this.macblackMap = macblackMap;
    }

    public Map<Object, Object> getIpaddrblackMap() {
        return ipaddrblackMap;
    }

    public void setIpaddrblackMap(Map<Object, Object> ipaddrblackMap) {
        this.ipaddrblackMap = ipaddrblackMap;
    }

    public Map<Object, Object> getMobileblackMap() {
        return mobileblackMap;
    }

    public void setMobileblackMap(Map<Object, Object> mobileblackMap) {
        this.mobileblackMap = mobileblackMap;
    }

    public Map<Object, Object> getEmailblackMap() {
        return emailblackMap;
    }

    public void setEmailblackMap(Map<Object, Object> emailblackMap) {
        this.emailblackMap = emailblackMap;
    }

    public Map<Object, Object> getUserIdblackMap() {
        return userIdblackMap;
    }

    public void setUserIdblackMap(Map<Object, Object> userIdblackMap) {
        this.userIdblackMap = userIdblackMap;
    }

    public Map<Object, Object> getThirdPartyIdblackMap() {
        return thirdPartyIdblackMap;
    }

    public void setThirdPartyIdblackMap(Map<Object, Object> thirdPartyIdblackMap) {
        this.thirdPartyIdblackMap = thirdPartyIdblackMap;
    }

    public Map<Object, Object> getAppIdblackMap() {
        return appIdblackMap;
    }

    public void setAppIdblackMap(Map<Object, Object> appIdblackMap) {
        this.appIdblackMap = appIdblackMap;
    }

    public List<String> getBlackList() {
        return blackList;
    }

    public void setBlackList(List<String> blackList) {
        this.blackList = blackList;
    }

    public List<String> getWhiteList() {
        return whiteList;
    }

    public void setWhiteList(List<String> whiteList) {
        this.whiteList = whiteList;
    }

    @Override
    public String toString() {
        return "IPBlackAndWhiteBean [appWhtelist=" + appWhtelist + ", macblackMap=" + macblackMap + ", ipaddrblackMap="
            + ipaddrblackMap + ", mobileblackMap=" + mobileblackMap + ", emailblackMap=" + emailblackMap
            + ", userIdblackMap=" + userIdblackMap + ", thirdPartyIdblackMap=" + thirdPartyIdblackMap
            + ", appIdblackMap=" + appIdblackMap + ", blackList=" + blackList + ", whiteList=" + whiteList + "]";
    }
}
